package cn.chiship.framework.upms.biz.system.controller;


import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.framework.common.util.FrameworkUtil2;
import cn.chiship.framework.upms.biz.base.service.CacheService;
import cn.chiship.framework.upms.biz.system.entity.UpmsConfig;
import cn.chiship.framework.upms.biz.system.entity.UpmsConfigExample;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsConfigDto;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsConfigGroupDto;
import cn.chiship.framework.upms.biz.system.pojo.dto.UpmsConfigSettingDto;
import cn.chiship.framework.upms.biz.system.service.UpmsConfigService;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.sdk.framework.util.FrameworkUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 系统配置控制层
 * 2021/9/27
 *
 * @author lijian
 */
@RestController
@Authorization
@RequestMapping("/systemConfig")
@Api(tags = "系统配置")
public class UpmsConfigController extends BaseController<UpmsConfig, UpmsConfigExample> {

    @Resource
    private UpmsConfigService upmsConfigService;
    @Resource
    private CacheService cacheService;

    @Override
    public BaseService getService() {
        return upmsConfigService;
    }

    @ApiOperation(value = "加载配置分组")
    @ApiImplicitParams({
    })
    @GetMapping(value = "group/list")
    public ResponseEntity<BaseResult> listGroup() {
        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        UpmsConfigExample.Criteria criteria = upmsConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(BaseConstants.NO).andTypeEqualTo(Byte.valueOf("0"));
        upmsConfigExample.setOrderByClause(FrameworkUtil.formatSort("+orders"));
        List<UpmsConfig> upmsConfigs = upmsConfigService.selectByExample(upmsConfigExample);
        return super.responseEntity(BaseResult.ok(FrameworkUtil2.assemblyDataTree("0", upmsConfigs)));
    }

    @ApiOperation(value = "新增配置分组")
    @PostMapping(value = "group/save")
    public ResponseEntity<BaseResult> groupSave(@RequestBody @Valid UpmsConfigGroupDto upmsConfigGroupDto) {
        return super.responseEntity(upmsConfigService.saveGroup(upmsConfigGroupDto));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = String.class, paramType = "path"),
    })
    @ApiOperation(value = "编辑配置分组")
    @PostMapping(value = "group/update/{id}")
    public ResponseEntity<BaseResult> groupUpdate(@PathVariable("id") String id, @RequestBody @Valid UpmsConfigGroupDto upmsConfigGroupDto) {
        return super.responseEntity(upmsConfigService.updateGroup(id, upmsConfigGroupDto));
    }

    @ApiOperation(value = "根据分组主键获取参数配置项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组ID",  required = true, dataTypeClass = String.class, paramType = "query"),
    })
    @GetMapping(value = "/listByGroupId")
    public ResponseEntity<BaseResult> loadByType(@RequestParam(value = "groupId") String groupId) {
        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        UpmsConfigExample.Criteria criteria = upmsConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(BaseConstants.NO).andPidEqualTo(groupId);
        upmsConfigExample.setOrderByClause(FrameworkUtil.formatSort("+orders"));
        List<UpmsConfig> upmsConfigs = upmsConfigService.selectByExample(upmsConfigExample);
        return super.responseEntity(BaseResult.ok(upmsConfigs));
    }

    @SystemOptionAnnotation(describe = "缓存配置", option = BusinessTypeEnum.SYSTEM_OPTION_OTHER)
    @ApiOperation(value = "缓存配置")
    @GetMapping(value = "/cacheConfig")
    public ResponseEntity<BaseResult> cacheConfig() {
        cacheService.cacheConfigs();
        return super.responseEntity(BaseResult.ok());
    }

    @ApiOperation(value = "新增配置项")
    @PostMapping(value = "save")
    public ResponseEntity<BaseResult> save(@RequestBody @Valid UpmsConfigDto upmsConfigDto) {
        UpmsConfig upmsConfig = new UpmsConfig();
        BeanUtils.copyProperties(upmsConfigDto, upmsConfig);
        upmsConfig.setType(Byte.valueOf("1"));
        return super.responseEntity(super.save(upmsConfig));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataTypeClass = String.class, paramType = "path"),
    })
    @ApiOperation(value = "编辑配置项")
    @PostMapping(value = "update/{id}")
    public ResponseEntity<BaseResult> update(@PathVariable("id") String id, @RequestBody @Valid UpmsConfigDto upmsConfigDto) {
        UpmsConfig upmsConfig = new UpmsConfig();
        BeanUtils.copyProperties(upmsConfigDto, upmsConfig);
        return super.responseEntity(super.update(id, upmsConfig));
    }


    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_UPDATE, describe = "修改配置参数值")
    @ApiOperation(value = "修改配置参数值")
    @PostMapping(value = "/updateConfig")
    public ResponseEntity<BaseResult> updateConfig(@RequestBody List<UpmsConfigSettingDto> configSettingDtos) {
        if (configSettingDtos.isEmpty()) {
            return super.responseEntity(BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_UPDATE, false));
        }
        return super.responseEntity(upmsConfigService.updateConfig(configSettingDtos));
    }

    @SystemOptionAnnotation(describe = "删除系统配置", option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE)
    @ApiOperation(value = "删除系统配置")
    @PostMapping(value = "/remove")
    public ResponseEntity<BaseResult> dataDictItemRemove(@RequestBody @Valid List<String> ids) {

        UpmsConfigExample upmsConfigExample = new UpmsConfigExample();
        upmsConfigExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(upmsConfigExample));
    }


}
